package com.raymond.client.msg;

import com.raymond.client.msg.response.BaseResponseMessage;
import com.raymond.client.service.Callback;
import com.raymond.client.thread.ThreadPool;
import com.raymond.client.utils.CachedMillisecondClock;


/**
 * 消息管理器
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-19 11:39
 */
public class MessageManages {
    /** 请求消息 **/
    private BaseMessage req;
    /** 响应数据 **/
    private BaseResponseMessage resp;
    /** 回调接口 **/
    private Callback callback;
    /** 响应编码 -1:递交失败 -2:没有响应 -3:获取窗口超时**/
    private int code = -1;
    /** 重试次数 **/
    private int retryCount = 3;
    /** 开始时间 **/
    private long startTime;

    /**
     * 消息管理器
     * @param req 请求
     */
    public MessageManages(BaseMessage req) {
        this.req = req;
    }

    /**
     * 消息管理器
     * @param req 请求
     * @param callback 回调接口
     */
    public MessageManages(BaseMessage req, Callback callback) {
        this.req = req;
        this.callback = callback;
        this.startTime = CachedMillisecondClock.INS.now();
    }

    /**
     * 获取请求
     * @return 请求
     */
    public BaseMessage getReq() {
        return req;
    }

    /**
     * 获取响应
     * @return 响应
     */
    public BaseResponseMessage getResp() {
        return resp;
    }


    public void setResp(BaseResponseMessage resp) {
        this.resp = resp;
    }

    /**
     * 等待
     * @param timeout 等待时间
     */
    public synchronized void wait0(long timeout) {
        if (isDone()) {
            return;
        }
        boolean interrupted = false;
        try {
            wait(timeout);
        } catch (InterruptedException e) {
            interrupted = true;
        } finally {
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }

    /**
     * 成功响应
     * @param resp 响应
     */
    public void success(BaseResponseMessage resp) {
        setResp(resp);
        if (isAsync()) {
            this.code = 0;
            addCallBackTask();
        } else {
            notify0();
        }
    }

    /**
     * 失败
     */
    public void asyncSendFail() {
        addCallBackTask();
    }

    /**
     * 添加到失败处理线程
     */
    public void addCallBackTask() {
        ThreadPool.CALLBACK.execute(()-> callback.callback(this));
//        callback.callback(this);
    }

    /**
     * 唤醒
     */
    private synchronized void notify0() {
        notify();
    }

    /**
     * 是否完成响应
     * @return true，已响应
     */
    public boolean isDone() {
        return resp != null;
    }

    /**
     * 是不是异步已响应
     * @return true:异步已响应
     */
    public boolean isAsyncDone() {
        return resp != null || code > 0;
    }

    /**
     * 是否可以重试
     * @return true:可以重试
     */
    public boolean isRetry() {
        return --retryCount > 0;
    }

    /**
     * 是否是异步
     * @return true:异步任务
     */
    public boolean isAsync() {
        return callback != null;
    }

    /**
     * 获取开始时间
     * @return 任务开始时间
     */
    public long getStartTime() {
        return startTime;
    }

    public MessageManages setStartTime(long startTime) {
        this.startTime = startTime;
        return this;
    }

    public int getCode() {
        return code;
    }

    public MessageManages setCode(int code) {
        this.code = code;
        return this;
    }
}
